<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>06-CSS盒模型详解</title>
  
    
      <meta 
        property="og:title" 
        content="06-CSS盒模型详解">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/06-CSS%E7%9B%92%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2002-01-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E6%A8%A1%E5%9E%8B"><span class="toc-text">盒子模型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E4%B8%AD%E7%9A%84%E5%8C%BA%E5%9F%9F"><span class="toc-text">盒子中的区域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E7%9B%92%E6%A8%A1%E5%9E%8B%E5%92%8CIE%E7%9B%92%E6%A8%A1%E5%9E%8B"><span class="toc-text">标准盒模型和IE盒模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#lt-body-gt-%E6%A0%87%E7%AD%BE%E4%B9%9F%E6%9C%89margin"><span class="toc-text">&lt;body&gt;标签也有margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86width%E3%80%81height"><span class="toc-text">认识width、height</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86padding"><span class="toc-text">认识padding</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E5%8C%BA%E5%9F%9F%E4%B9%9F%E6%9C%89%E9%A2%9C%E8%89%B2"><span class="toc-text">padding区域也有颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E6%9C%89%E5%9B%9B%E4%B8%AA%E6%96%B9%E5%90%91"><span class="toc-text">padding有四个方向</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E9%A2%98%E7%9B%AE"><span class="toc-text">一些题目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E5%85%83%E7%B4%A0%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%B8%A6%E6%9C%89padding"><span class="toc-text">一些元素，默认带有padding</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86border"><span class="toc-text">认识border</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#border-style"><span class="toc-text">border-style</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border%E6%8B%86%E5%88%86"><span class="toc-text">border拆分</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border-image-%E5%B1%9E%E6%80%A7"><span class="toc-text">border-image 属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例1：利用 border 属性画一个三角形（小技巧）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E6%9B%B4%E6%8E%A8%E8%8D%90%E7%9A%84%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例2：利用 border 属性画一个三角形（更推荐的技巧）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      06-CSS盒模型详解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2001-12-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2002-01-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          CSS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>2.6k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            CSS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="盒子模型"><a href="#盒子模型" class="headerlink" title="盒子模型"></a>盒子模型</h2><h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>盒子模型，英文即box model。无论是div、span、还是a都是盒子。</p>
<p>但是，图片、表单元素一律看作是文本，它们并不是盒子。这个很好理解，比如说，一张图片里并不能放东西，它自己就是自己的内容。</p>
<h3 id="盒子中的区域"><a href="#盒子中的区域" class="headerlink" title="盒子中的区域"></a>盒子中的区域</h3><p>一个盒子中主要的属性就5个：width、height、padding、border、margin。如下：</p>
<ul>
<li>width和height：<strong>内容</strong>的宽度、高度（不是盒子的宽度、高度）。</li>
<li>padding：内边距。</li>
<li>border：边框。</li>
<li>margin：外边距。</li>
</ul>
<p>盒子模型的示意图：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2128.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2128.png"></p>
<p>代码演示：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2326.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2326.png"></p>
<p>上面这个盒子，width:200px; height:200px; 但是真实占有的宽高是302*302。 这是因为还要加上padding、border。</p>
<p>注意：<strong>宽度和真实占有宽度，不是一个概念！</strong>来看下面这例子。</p>
<h3 id="标准盒模型和IE盒模型"><a href="#标准盒模型和IE盒模型" class="headerlink" title="标准盒模型和IE盒模型"></a>标准盒模型和IE盒模型</h3><blockquote>
<p>我们目前所学习的知识中，以标准盒子模型为准。</p>
</blockquote>
<p>标准盒子模型：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-27.jpg" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-27.jpg"></p>
<p>IE盒子模型：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-30.jpg" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-30.jpg"></p>
<p>上图显示：</p>
<p>在 CSS 盒子模型 (Box Model) 规定了元素处理元素的几种方式：</p>
<ul>
<li>width和height：<strong>内容</strong>的宽度、高度（不是盒子的宽度、高度）。</li>
<li>padding：内边距。</li>
<li>border：边框。</li>
<li>margin：外边距。</li>
</ul>
<p>CSS盒模型和IE盒模型的区别：</p>
<ul>
<li><p>在 <font color="#0000FF"><strong>标准盒子模型</strong></font>中，<font color="#0000FF"><strong>width 和 height 指的是内容区域</strong></font>的宽度和高度。增加内边距、边框和外边距不会影响内容区域的尺寸，但是会增加元素框的总尺寸。</p>
</li>
<li><p><font color="#0000FF"><strong>IE盒子模型</strong></font>中，<font color="#0000FF"><strong>width 和 height 指的是内容区域+border+padding</strong></font>的宽度和高度。</p>
</li>
</ul>
<p>注：Android中也有margin和padding的概念，意思是差不多的，如果你会一点Android，应该比较好理解吧。区别在于，Android中没有border这个东西，而且在Android中，margin并不是控件的一部分。</p>
<h3 id="lt-body-gt-标签也有margin"><a href="#lt-body-gt-标签也有margin" class="headerlink" title="&lt;body&gt;标签也有margin"></a><code>&lt;body&gt;</code>标签也有margin</h3><p><code>&lt;body&gt;</code>标签有必要强调一下。很多人以为<code>&lt;body&gt;</code>标签占据的是整个页面的全部区域，其实是错误的，正确的理解是这样的：整个网页最大的盒子是<code>&lt;document&gt;</code>，即浏览器。而<code>&lt;body&gt;</code>是<code>&lt;document&gt;</code>的儿子。浏览器给<code>&lt;body&gt;</code>默认的margin大小是8个像素，此时<code>&lt;body&gt;</code>占据了整个页面的一大部分区域，而不是全部区域。来看一段代码。</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!doctype <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;Generator&quot;</span> <span class="attr">content</span>=<span class="string">&quot;EditPlus®&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;Author&quot;</span> <span class="attr">content</span>=<span class="string">&quot;&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;Keywords&quot;</span> <span class="attr">content</span>=<span class="string">&quot;&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;Description&quot;</span> <span class="attr">content</span>=<span class="string">&quot;&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		<span class="selector-tag">div</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">border</span>: <span class="number">1px</span> solid red;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">padding</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">	</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"></span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span>&gt;</span>有生之年<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span>&gt;</span>狭路相逢<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>

<p>上面的代码中，我们对div标签设置了边距等信息。打开google浏览器，按住F12，显示效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20151003_27.png" class="lozad post-image"src="http://img.smyhvae.com/20151003_27.png"></p>
<h2 id="认识width、height"><a href="#认识width、height" class="headerlink" title="认识width、height"></a>认识width、height</h2><p>一定要知道，在前端开发工程师眼中，世界中的一切都是不同的。</p>
<p>比如说，丈量稿纸，前端开发工程师只会丈量内容宽度：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2329.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2329.png"></p>
<p>下面这两个盒子，真实占有宽高，都是302*302：</p>
<p>盒子1：</p>
<pre class="highlight"><span class="line"><span class="selector-class">.box1</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">padding</span>: <span class="number">100px</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br></pre>

<p>盒子2：</p>
<pre class="highlight"><span class="line"><span class="selector-class">.box2</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">250px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">250px</span>;</span><br><span class="line">	<span class="attribute">padding</span>: <span class="number">25px</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br></pre>


<p>真实占有宽度 &#x3D; 左border + 左padding + width + 右padding + 右border</p>
<p>上面这两个盒子的盒模型图如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20170728_0925.png" class="lozad post-image"src="https://img.smyhvae.com/20170728_0925.png"></p>
<p><strong>如果想保持一个盒子的真实占有宽度不变，那么加width的时候就要减padding。加padding的时候就要减width</strong>。因为盒子变胖了是灾难性的，这会把别的盒子挤下去。</p>
<h2 id="认识padding"><a href="#认识padding" class="headerlink" title="认识padding"></a>认识padding</h2><h3 id="padding区域也有颜色"><a href="#padding区域也有颜色" class="headerlink" title="padding区域也有颜色"></a>padding区域也有颜色</h3><p>padding就是内边距。padding的区域有背景颜色，css2.1前提下，并且背景颜色一定和内容区域的相同。也就是说，background-color将填充<strong>所有border以内的区域。</strong></p>
<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1005.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1005.png"></p>
<h3 id="padding有四个方向"><a href="#padding有四个方向" class="headerlink" title="padding有四个方向"></a>padding有四个方向</h3><p>padding是4个方向的，所以我们能够分别描述4个方向的padding。</p>
<p>方法有两种，第一种写小属性；第二种写综合属性，用空格隔开。</p>
<p>小属性的写法：</p>
<pre class="highlight"><span class="line"><span class="attribute">padding-top</span>: <span class="number">30px</span>;</span><br><span class="line"><span class="attribute">padding-right</span>: <span class="number">20px</span>;</span><br><span class="line"><span class="attribute">padding-bottom</span>: <span class="number">40px</span>;</span><br><span class="line"><span class="attribute">padding-left</span>: <span class="number">100px</span>;</span><br></pre>

<p>综合属性的写法：(上、右、下、左)（顺时针方向，用空格隔开。margin的道理也是一样的）</p>
<pre class="highlight"><span class="line"><span class="attribute">padding</span>:<span class="number">30px</span> <span class="number">20px</span> <span class="number">40px</span> <span class="number">100px</span>;</span><br></pre>

<p>如果写了四个值，则顺序为：上、右、下、左。</p>
<p>如果只写了三个值，则顺序为：上、右和左、下。</p>
<p>如果只写了两个值，则顺序为：上和下、左和右。</p>
<p>比如说：</p>
<pre class="highlight"><span class="line">padding: 30px 40px;</span><br></pre>

<p>则顺序等价于：30px 40px 30px 40px;</p>
<p>要懂得，<strong>用小属性层叠大属性</strong>。比如：</p>
<pre class="highlight"><span class="line">padding: 20px;</span><br><span class="line">padding-left: 30px;</span><br></pre>

<p>上面的padding对应盒子模型为：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1039.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1039.png"></p>
<p>下面的写法：</p>
<pre class="highlight"><span class="line">padding-left: 30px;</span><br><span class="line">padding: 20px;</span><br></pre>

<p>第一行的小属性无效，因为被第二行的大属性层叠掉了。</p>
<p>下面的题，会做了，说明你明白了。</p>
<h3 id="一些题目"><a href="#一些题目" class="headerlink" title="一些题目"></a>一些题目</h3><p><strong>题目1</strong>：说出下面盒子真实占有宽高，并画出盒模型图。</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">	<span class="attribute">padding</span>: <span class="number">10px</span> <span class="number">20px</span> <span class="number">30px</span>;</span><br><span class="line">	<span class="attribute">padding-right</span>: <span class="number">40px</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>答案：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1048.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1048.png"></p>
<p><strong>题目2</strong>：说出下面盒子真实占有宽高，并画出盒模型图。</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">	<span class="attribute">padding-left</span>: <span class="number">10px</span>;</span><br><span class="line">	<span class="attribute">padding-right</span>: <span class="number">20px</span>;</span><br><span class="line">	<span class="attribute">padding</span>:<span class="number">40px</span> <span class="number">50px</span> <span class="number">60px</span>;</span><br><span class="line">	<span class="attribute">padding-bottom</span>: <span class="number">30px</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>答案：</p>
<p><code>padding-left:10px；</code> 和<code>padding-right:20px;</code> 没用，因为后面的padding大属性，层叠掉了他们。</p>
<p>盒子模型如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1100.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1100.png"></p>
<p><strong>题目3</strong>：现在给你一个盒子模型图，请写出代码，试着用最最简单的方法写。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1401.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1401.png"></p>
<p>答案：</p>
<pre class="highlight"><span class="line"><span class="attribute">width</span>:<span class="number">123px</span>;</span><br><span class="line"><span class="attribute">height</span>:<span class="number">123px</span>;</span><br><span class="line"><span class="attribute">padding</span>:<span class="number">20px</span> <span class="number">40px</span>;</span><br><span class="line"><span class="attribute">border</span>:<span class="number">1px</span> solid red;</span><br></pre>

<p><strong>题目4</strong>：现在给你一个盒子模型图，请写出代码，试着用最最简单的方法写。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1402.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1402.png"></p>
<p>答案：</p>
<pre class="highlight"><span class="line"><span class="attribute">width</span>:<span class="number">123px</span>;</span><br><span class="line"><span class="attribute">height</span>:<span class="number">123px</span>;</span><br><span class="line"><span class="attribute">padding</span>:<span class="number">20px</span>;</span><br><span class="line"><span class="attribute">padding-right</span>:<span class="number">40px</span>;</span><br><span class="line"><span class="attribute">border</span>:<span class="number">1px</span> solid red;</span><br><span class="line"></span><br></pre>

<h3 id="一些元素，默认带有padding"><a href="#一些元素，默认带有padding" class="headerlink" title="一些元素，默认带有padding"></a>一些元素，默认带有padding</h3><p>一些元素，默认带有<code>padding</code>，比如ul标签。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1413.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1413.png"></p>
<p>上图显示，不加任何样式的ul，也是有40px的padding-left。</p>
<p>所以，我们做站的时候，为了便于控制，总是喜欢清除这个默认的padding。</p>
<p>可以使用<code>*</code>进行清除：</p>
<pre class="highlight"><span class="line">*&#123;</span><br><span class="line">	<span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">	<span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre>

<p> 但是，<code>*</code>的效率不高，所以我们使用并集选择器，罗列所有的标签（不用背，有专业的清除默认样式的样式表，今后学习）：</p>
<pre class="highlight"><span class="line">body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td&#123;</span><br><span class="line">    margin:0;</span><br><span class="line">    padding:0;</span><br><span class="line">&#125;</span><br></pre>

<h2 id="认识border"><a href="#认识border" class="headerlink" title="认识border"></a>认识border</h2><p>border就是边框。边框有三个要素：像素（粗细）、线型、颜色。</p>
<p>比如：</p>
<pre class="highlight"><span class="line"></span><br><span class="line"><span class="selector-class">.div1</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">10px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">10px</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">2px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<p>颜色如果不写，默认是黑色。另外两个属性如果不写，则无法显示边框。</p>
<h3 id="border-style"><a href="#border-style" class="headerlink" title="border-style"></a>border-style</h3><p>border的所有的线型如下：（我们可以通过查看<code>CSS参考手册</code>得到）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1435.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1435.png"></p>
<p>比如<code>border:10px ridge red;</code>这个属性，在chrome和firefox、IE中有细微差别：（因为可以显示出效果，因此并不是兼容性问题，只是有细微差别而已）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1619.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1619.png"></p>
<p>如果公司里面的设计师是处女座的，追求极高的<strong>页面还原度</strong>，那么不能使用css来制作边框。就要用到图片，就要切图了。</p>
<p>所以，比较稳定的border-style就几个：solid、dashed、dotted。</p>
<h3 id="border拆分"><a href="#border拆分" class="headerlink" title="border拆分"></a>border拆分</h3><p>border是一个大综合属性。比如说：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:<span class="number">1px</span> solid red;</span><br></pre>

<p>就是把上下左右这四个方向的边框，都设置为 1px 宽度、线型实线、red颜色。</p>
<p>PS：小技巧：在sublime text中，为了快速输入<code>border:1px solid red;</code>这个属性，可以直接输入<code>bd</code>，然后选第二个后回车。</p>
<p>border属性是能够被拆开的，有两大种拆开的方式：</p>
<ul>
<li><p>（1）按三要素拆开：border-width、border-style、border-color。（一个border属性是由三个小属性综合而成的）</p>
</li>
<li><p>（2）按方向拆开：border-top、border-right、border-bottom、border-left。</p>
</li>
</ul>
<p>现在我们明白了：<strong>一个border属性，是由三个小属性综合而成的</strong>。如果某一个小属性后面是空格隔开的多个值，那么就是上右下左的顺序。举例如下：</p>
<pre class="highlight"><span class="line">border-width:10px 20px;</span><br><span class="line">border-style:solid dashed dotted;</span><br><span class="line">border-color:red green blue yellow;</span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1516.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1516.png"></p>
<p>（1）按三要素拆：</p>
<pre class="highlight"><span class="line"><span class="attribute">border-width</span>:<span class="number">10px</span>;    //边框宽度</span><br><span class="line"><span class="attribute">border-style</span>:solid;   //线型</span><br><span class="line"><span class="attribute">border-color</span>:red;     //颜色。</span><br></pre>
<p>等价于：</p>
<pre class="highlight"><span class="line">border:10px solid red;</span><br></pre>

<p>(2)按方向来拆：</p>
<pre class="highlight"><span class="line"><span class="attribute">border-top</span>:<span class="number">10px</span> solid red;</span><br><span class="line"><span class="attribute">border-right</span>:<span class="number">10px</span> solid red;</span><br><span class="line"><span class="attribute">border-bottom</span>:<span class="number">10px</span> solid red;</span><br><span class="line"><span class="attribute">border-left</span>:<span class="number">10px</span> solid red;</span><br></pre>

<p>等价于：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:<span class="number">10px</span> solid red;</span><br></pre>

<p>（3）按三要素和方向来拆：(就是把每个方向的，每个要素拆开。3*4 &#x3D; 12)</p>
<pre class="highlight"><span class="line"><span class="attribute">border-top-width</span>:<span class="number">10px</span>;</span><br><span class="line"><span class="attribute">border-top-style</span>:solid;</span><br><span class="line"><span class="attribute">border-top-color</span>:red;</span><br><span class="line"><span class="attribute">border-right-width</span>:<span class="number">10px</span>;</span><br><span class="line"><span class="attribute">border-right-style</span>:solid;</span><br><span class="line"><span class="attribute">border-right-color</span>:red;</span><br><span class="line"><span class="attribute">border-bottom-width</span>:<span class="number">10px</span>;</span><br><span class="line"><span class="attribute">border-bottom-style</span>:solid;</span><br><span class="line"><span class="attribute">border-bottom-color</span>:red;</span><br><span class="line"><span class="attribute">border-left-width</span>:<span class="number">10px</span>;</span><br><span class="line"><span class="attribute">border-left-style</span>:solid;</span><br><span class="line"><span class="attribute">border-left-color</span>:red;</span><br></pre>

<p>等价于：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:<span class="number">10px</span> solid red;</span><br><span class="line"></span><br></pre>

<p>工作中到底用什么？很简答：什么简单用什么。但要懂得，用小属性层叠大属性。举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1606.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1606.png"></p>
<p>为了实现上方效果，写法如下：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:<span class="number">10px</span> solid red;</span><br><span class="line"><span class="attribute">border-right-color</span>:blue;</span><br></pre>

<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1608.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1608.png"></p>
<p>为了实现上方效果，写法如下：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:<span class="number">10px</span> solid red;</span><br><span class="line"><span class="attribute">border-style</span>:solid dashed;</span><br></pre>

<p>border可以没有：</p>
<pre class="highlight"><span class="line"><span class="attribute">border</span>:none;</span><br></pre>

<p>可以某一条边没有：</p>
<pre class="highlight"><span class="line"><span class="attribute">border-left</span>: none;</span><br></pre>

<p>也可以调整左边边框的宽度为0：</p>
<pre class="highlight"><span class="line"><span class="attribute">border-left-width</span>: <span class="number">0</span>;</span><br></pre>

<h3 id="border-image-属性"><a href="#border-image-属性" class="headerlink" title="border-image 属性"></a>border-image 属性</h3><p>比如：</p>
<pre class="highlight"><span class="line"><span class="attribute">border-image</span>: <span class="built_in">url</span>(<span class="string">.img.png</span>) <span class="number">30</span> round;</span><br></pre>

<p>这个属性在实际开发中用得不多，暂时忽略。</p>
<h3 id="举例1：利用-border-属性画一个三角形（小技巧）"><a href="#举例1：利用-border-属性画一个三角形（小技巧）" class="headerlink" title="举例1：利用 border 属性画一个三角形（小技巧）"></a>举例1：利用 border 属性画一个三角形（小技巧）</h3><p>完整代码如下：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">0</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">0</span>;</span><br><span class="line">	<span class="attribute">border</span>: <span class="number">50px</span> solid transparent;</span><br><span class="line">	<span class="attribute">border-top-color</span>: red;</span><br><span class="line">	<span class="attribute">border-bottom</span>: none;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<p>步骤如下：</p>
<p>（1）当我们设置盒子的width和height为0时，此时效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1639.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1639.png"></p>
<p>（2）然后将border的底部取消：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1645.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1645.png"></p>
<p>（3）最后设置border的左边和右边为白色或者<strong>透明</strong>：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170728_1649.png" class="lozad post-image"src="http://img.smyhvae.com/20170728_1649.png"></p>
<p>这样，一个三角形就画好了。</p>
<h3 id="举例2：利用-border-属性画一个三角形（更推荐的技巧）"><a href="#举例2：利用-border-属性画一个三角形（更推荐的技巧）" class="headerlink" title="举例2：利用 border 属性画一个三角形（更推荐的技巧）"></a>举例2：利用 border 属性画一个三角形（更推荐的技巧）</h3><p>上面的例子1中，画出来的是直角三角形，可如果我想画等边三角形，要怎么做呢？</p>
<p>完整代码如下：（用 css 画等边三角形）</p>
<pre class="highlight"><span class="line"><span class="selector-class">.div1</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">0</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">0</span>;</span><br><span class="line">	<span class="attribute">border-top</span>: <span class="number">30px</span> solid red;</span><br><span class="line">	<span class="comment">/* 通过改变 border-left 和 border-right 中的像素值，来改变三角形的形状 */</span></span><br><span class="line">	<span class="attribute">border-left</span>: <span class="number">20px</span> solid transparent;</span><br><span class="line">	<span class="attribute">border-right</span>: <span class="number">20px</span> solid transparent;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20191004_1830.png" class="lozad post-image"src="http://img.smyhvae.com/20191004_1830.png"></p>
<p>另外，我们在上方代码的基础之上，再加一个 <code>border-radus: 20px;</code> 就能画出一个扇形。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/06-CSS%E7%9B%92%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3/">
            https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/06-CSS%E7%9B%92%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/05-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7%E5%92%8C%E5%B1%82%E5%8F%A0%E6%80%A7/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">05-CSS样式表的继承性和层叠性 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/07-%E6%B5%AE%E5%8A%A8/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">07-浮动 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E6%A8%A1%E5%9E%8B"><span class="toc-text">盒子模型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E4%B8%AD%E7%9A%84%E5%8C%BA%E5%9F%9F"><span class="toc-text">盒子中的区域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E7%9B%92%E6%A8%A1%E5%9E%8B%E5%92%8CIE%E7%9B%92%E6%A8%A1%E5%9E%8B"><span class="toc-text">标准盒模型和IE盒模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#lt-body-gt-%E6%A0%87%E7%AD%BE%E4%B9%9F%E6%9C%89margin"><span class="toc-text">&lt;body&gt;标签也有margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86width%E3%80%81height"><span class="toc-text">认识width、height</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86padding"><span class="toc-text">认识padding</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E5%8C%BA%E5%9F%9F%E4%B9%9F%E6%9C%89%E9%A2%9C%E8%89%B2"><span class="toc-text">padding区域也有颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E6%9C%89%E5%9B%9B%E4%B8%AA%E6%96%B9%E5%90%91"><span class="toc-text">padding有四个方向</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E9%A2%98%E7%9B%AE"><span class="toc-text">一些题目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E5%85%83%E7%B4%A0%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%B8%A6%E6%9C%89padding"><span class="toc-text">一些元素，默认带有padding</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86border"><span class="toc-text">认识border</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#border-style"><span class="toc-text">border-style</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border%E6%8B%86%E5%88%86"><span class="toc-text">border拆分</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border-image-%E5%B1%9E%E6%80%A7"><span class="toc-text">border-image 属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例1：利用 border 属性画一个三角形（小技巧）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E6%9B%B4%E6%8E%A8%E8%8D%90%E7%9A%84%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例2：利用 border 属性画一个三角形（更推荐的技巧）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E6%A8%A1%E5%9E%8B"><span class="toc-text">盒子模型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E4%B8%AD%E7%9A%84%E5%8C%BA%E5%9F%9F"><span class="toc-text">盒子中的区域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E7%9B%92%E6%A8%A1%E5%9E%8B%E5%92%8CIE%E7%9B%92%E6%A8%A1%E5%9E%8B"><span class="toc-text">标准盒模型和IE盒模型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#lt-body-gt-%E6%A0%87%E7%AD%BE%E4%B9%9F%E6%9C%89margin"><span class="toc-text">&lt;body&gt;标签也有margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86width%E3%80%81height"><span class="toc-text">认识width、height</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86padding"><span class="toc-text">认识padding</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E5%8C%BA%E5%9F%9F%E4%B9%9F%E6%9C%89%E9%A2%9C%E8%89%B2"><span class="toc-text">padding区域也有颜色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#padding%E6%9C%89%E5%9B%9B%E4%B8%AA%E6%96%B9%E5%90%91"><span class="toc-text">padding有四个方向</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E9%A2%98%E7%9B%AE"><span class="toc-text">一些题目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%BA%9B%E5%85%83%E7%B4%A0%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%B8%A6%E6%9C%89padding"><span class="toc-text">一些元素，默认带有padding</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A4%E8%AF%86border"><span class="toc-text">认识border</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#border-style"><span class="toc-text">border-style</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border%E6%8B%86%E5%88%86"><span class="toc-text">border拆分</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#border-image-%E5%B1%9E%E6%80%A7"><span class="toc-text">border-image 属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例1：利用 border 属性画一个三角形（小技巧）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E5%88%A9%E7%94%A8-border-%E5%B1%9E%E6%80%A7%E7%94%BB%E4%B8%80%E4%B8%AA%E4%B8%89%E8%A7%92%E5%BD%A2%EF%BC%88%E6%9B%B4%E6%8E%A8%E8%8D%90%E7%9A%84%E6%8A%80%E5%B7%A7%EF%BC%89"><span class="toc-text">举例2：利用 border 属性画一个三角形（更推荐的技巧）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
